library(ggplot2)
library(dplyr)
library(modelsummary)
library(kableExtra)
library(stringr)
library(fixest)
library(stargazer)
library(gridExtra)
library(flextable)
library(Hmisc)
library(tidyr)

df <- readRDS("data/df_long.rds") %>% 
  ungroup() %>% 
  select(meta_UUID, 
         party_01_closest, 
         party_02_closest_party,
         party_03_slightly_closer,
         party_04_voted,
         party_affil) %>% 
  .[!duplicated(.), ]

############### Column Names ##############

# party_affil: 
# If closest party indicate -> party_02_closest_party
# else party_03_slightly_closter
# If party_affil still empty (or "No") -> party_04_voted

df <- df %>% 
  mutate(party_affil = ifelse(party_01_closest == "Yes", 
                              party_02_closest_party, NA),
         party_affil = ifelse(is.na(party_affil) & !is.na(party_03_slightly_closer), 
                              party_03_slightly_closer, party_affil),
         d_affil_leaners = ifelse(!is.na(party_affil) & party_affil != "" & party_affil != "No", 
                                  1, 0), 
         d_affil_voted = ifelse(is.na(party_affil) | party_affil == "" | party_affil == "No", 
                                1, 0), 
         d_affil_check = d_affil_voted + d_affil_leaners)

df$party_affil <- recode(df$party_affil, 
                         "Synaspismós Rizospastikís Aristerás – Proodeftikí Symmachía (SYRIZA)" = 
                           "SYRIZA")

affil_dummies <- df %>% select(meta_UUID, d_affil_voted, d_affil_leaners)



survey_long <- readRDS("data/df_long.rds")

survey_long <- survey_long %>% left_join(affil_dummies) %>% 
  mutate(d_affil_voted = ifelse(d_affil_voted == 1 & d_affiliated == 0, 
                                0, d_affil_voted))

survey_long <- survey_long %>% filter(d_affiliated == 1 & d_ownparty == 0 & d_affil_leaners == 1)
survey_long <- survey_long %>% 
  group_by(meta_UUID) %>%
  mutate(N_ind = n(), 
         weights = meta_Weight / N_ind,
         weights_manual = manual_weights2 / N_ind) 


sd(survey_long$feeling_toward)

########### Regression analyses

m1 <- feols(close_num ~ feeling_toward, survey_long,
            cluster = "dem_country_code", weights = survey_long$weights)
m2 <- feols(close_num ~ feeling_toward | dem_country_code, survey_long,
            weights = survey_long$weights)
m3 <- feols(close_num ~ feeling_toward | meta_UUID, survey_long %>% filter(dem_country_code != "US"), 
            weights = survey_long$weights[survey_long$dem_country_code != "US"])

options("modelsummary_format_numeric_latex" = "plain")
models <- list("(1)" = m1, "(2)" = m2, "(3)" = m3)
tab <- modelsummary(models, output = 'tables/TableA9.html', stars = TRUE,
                    coef_rename = c("feeling_toward" = "Feeling", 
                                    "dem_country_code" = "Country-FE", 
                                    "meta_UUID" = "Individual-FE"),
                    coef_omit = c("(Intercept)"),
                    gof_omit = "AIC|BIC|Log|Pseudo|Adj|Within|Std.Errors",
                    statistic = "p.value")
